package mvc_generator.generator.car.jsf

import mvc_generator.generator.MVCGeneratorGeneratorUtils
import mvc_generator.mVCGenerator.BooleanType
import mvc_generator.mVCGenerator.Condition
import mvc_generator.mVCGenerator.Controller
import mvc_generator.mVCGenerator.DateType
import mvc_generator.mVCGenerator.Declaration
import mvc_generator.mVCGenerator.DoubleType
import mvc_generator.mVCGenerator.Form
import mvc_generator.mVCGenerator.FormValidation
import mvc_generator.mVCGenerator.IntegerType
import mvc_generator.mVCGenerator.ModelMVC
import mvc_generator.mVCGenerator.SimpleType
import mvc_generator.mVCGenerator.StringType
import org.eclipse.xtext.generator.IFileSystemAccess

class ControllerGenerator {

	def void generate(Controller controller, IFileSystemAccess fsa) {

		var ModelMVC modelMVCContainer = MVCGeneratorGeneratorUtils.findModelMVCContainer(controller);
		var ExpressionGenerator expressionGenerator = new ExpressionGenerator();
		var CharSequence fileContent = null;

		if (modelMVCContainer == null) {
			fileContent = '''@GenerationError: WebSite container not found for this controller'''
		} else {
			fileContent = '''
				package «JSFConfigConstantes.BEANCLASSES_PACKAGE.replace('/', '.')»;
				
				import javax.faces.bean.ManagedBean;
				
				@ManagedBean
				public class «controller.name.toFirstUpper» {
					
					«FOR Declaration declaration : controller.declarations»
						private «computeStringRepresentation(declaration.type)» «declaration.name»; 
					«ENDFOR»
					
					
					«FOR FormValidation formValidation : controller.formValidations»
						public String validate«controller.controlledView.name.toFirstUpper»«formValidation.validatedFormName.toFirstUpper»(){
						
							Boolean success = true;
							
							«formValidation.validatedFormName.toFirstLower»Error= "";
							
							«FOR Condition condition : formValidation.conditions»
								if((«expressionGenerator.generate(condition.condValue)») == false){
									
									«formValidation.validatedFormName.toFirstLower»Error += "Error: «condition.errorDescription»\n";
									return "«controller.controlledView.name».xhtml";
								}
							«ENDFOR»
						
							/* Mettez votre code de traitement. Toutes les pages ont la forme "<yourPageName>.xhtml" */
						
							return "«modelMVCContainer.config.indexView.name».xhtml";
						}
						
					«ENDFOR»
				
					
					«FOR Declaration declaration : controller.declarations»
					public «computeStringRepresentation(declaration.type)» get«declaration.name.toFirstUpper»(){ 
						return «declaration.name»;
					} 
					public void set«declaration.name.toFirstUpper»(«computeStringRepresentation(declaration.type)» new«declaration.
					name.toFirstUpper»){
						this.«declaration.name»= new«declaration.name.toFirstUpper»;
					}
					«ENDFOR»
					
					«FOR Form form : controller.controlledView.forms»
					private String «form.name.toFirstLower»Error;
					«ENDFOR»
					
					
					«FOR Form form : controller.controlledView.forms»
					public String get«form.name.toFirstUpper»Error(){ 
						return «form.name.toFirstLower»Error;
					}
					
					public void set«form.name.toFirstUpper»Error(String new«form.name.toFirstUpper»Error){
						this.«form.name.toFirstLower»Error = new«form.name.toFirstUpper»Error;
					}
					«ENDFOR»
				}
			'''
		}

		fsa.generateFile(
			modelMVCContainer.name + "Jsf/src/beans/" + controller.controlledView.name.toFirstUpper() + "Controller.java",
			fileContent);
	}

	private def String computeStringRepresentation(SimpleType simpleType) {
		var String stringRepresentation = "";

		if (simpleType instanceof IntegerType) {
			stringRepresentation = "Integer";
		} else if (simpleType instanceof DoubleType) {
			stringRepresentation = "Double";
		} else if (simpleType instanceof StringType) {
			stringRepresentation = "String"
		} else if (simpleType instanceof BooleanType) {
			stringRepresentation = "Boolean";
		} else if (simpleType instanceof DateType) {
			stringRepresentation = "Date"
		} else {
			stringRepresentation = '''@AutoGenerate_Error: «simpleType.getClass.simpleName» is not a handled SimpleType'''
		}

		return stringRepresentation;
	}
}
